<!DOCTYPE html>
<html class="x-admin-sm">
    
    <head>
        <meta charset="UTF-8">
        <title></title>
        <meta name="renderer" content="webkit">
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
        <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
        <link rel="stylesheet" href="./css/font.css">
        <link rel="stylesheet" href="./css/xadmin.css">
        <script src="js/jquery.min.js?v=2.1.4"></script>
        <script type="text/javascript" src="./lib/layui/layui.js" charset="utf-8"></script>
        <script type="text/javascript" src="./js/xadmin.js"></script>
        <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
        <!--[if lt IE 9]>
            <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
            <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
        <![endif]-->
    </head>
    <body>
        <div class="layui-fluid">
            <div class="layui-row">
                <form class="layui-form">
                    <div class="layui-form-item">
                        <label for="L_email" class="layui-form-label">
                            <span class="x-red">*</span>商品名称</label>
                        <div class="layui-input-inline">
                            <input type="text" id="name" name="name" required="" lay-verify="name" autocomplete="off" class="layui-input"></div>
                        </div>
                    
                    <div class="layui-form-item">
                        <label for="L_username" class="layui-form-label">
                            <span class="x-red">*</span>价格</label>
                        <div class="layui-input-inline">
                            <input type="text" id="price" name="price" required="" lay-verify="price" autocomplete="off" class="layui-input"></div>
                    </div>



                    <div class="layui-form-item layui-form-text">
                        <label for="L_pass" class="layui-form-label">
                            <span class="x-red">*</span>产品卖点</label>
                        <div class="layui-input-block">
                        	<input type="text" id="selling1" name="selling1"  lay-verify="" autocomplete="off" class="layui-input">
                        	<input type="text" id="selling2" name="selling2"  lay-verify="" autocomplete="off" class="layui-input">
                        	<input type="text" id="selling3" name="selling3"  lay-verify="" autocomplete="off" class="layui-input">
                        	                        	
                        </div>
                    </div>
                    <div class="layui-form-item layui-form-text">
                        <label for="L_pass" class="layui-form-label">
                            <span class="x-red">*</span>内容</label>
                        <div class="layui-input-block">
                            <textarea id="content" name="content" placeholder="请输入内容" class="layui-textarea" spellcheck="false"></textarea></div>
                    </div>
                    <div class="layui-form-item">
                        <label for="L_repass" class="layui-form-label"></label>
                        <button class="layui-btn" lay-filter="modify" lay-submit="">修改</button></div>
                </form>
            </div>
        </div>
        <input id="id" type="hidden">
        <script>

        layui.use(['form', 'layer','jquery'],
            function() {
                $ = layui.jquery;
                var form = layui.form,
                layer = layui.layer;
                //自定义验证规则
                form.verify({
                    price: function(value) {
                    	if (value.length==0) {
                            return '请输入价格';
                        }
                        if (value.length > 6) {
                            return '价格不能超过10万';
                        }
                    },
                });

                //监听提交
                form.on('submit(modify)',
                function(data) {
                    console.log(data);
                    //发异步，把数据提交给php
                    doSubmit();
                    
                    return false;
                });

            });
        layui.use('upload', function(){
        	  var upload = layui.upload;
        	   
        	  //执行实例
        	  var uploadInst = upload.render({
        	    elem: '#uploadImg' //绑定元素
        	    ,url: 'goods/upload' //上传接口
        	    ,field:"image"
        	    ,done: function(res){
        	      //上传完毕回调
        	    	if(res.code==0){
                        $("#img").attr('src',res.data.imageURL);
                        imageURL=res.data.dbURL;
                    }else{
                        //alert(res.msg);
                        layer.msg(res.msg);
                    }
        	    }
        	    ,error: function(){
        	      //请求异常回调
        	    }
        	  });
        	});</script>
<script type="text/javascript">

var imageURL="";
function doSubmit(){
	layer.confirm('您确定要修改吗？', {
		  btn: ['确定','取消'] //按钮
		}, function(){
			modify();
		}, function(){
		  
		});
}
function modify(){
	var id=$("#id").val();
	var price=$("#price").val();

	var jonsStr=JSON.stringify({
        "id":id,
        "purchase_price":price
    });
	$.ajax({   
           url:'/mgr/goods',
           type:'put',
           dataType:'json',//服务器端返回的数据格式是json
           contentType: 'application/json;charset=utf-8',
		   data: jonsStr ,//发给服务器端的数据
           success:function(res){   //data:服务器端返回给浏览器端的数据
		   		if(res.code==0){
		   			layer.alert(res.msg, {
                        icon: 6
                    },
                    function() {
                        //关闭当前frame
                        xadmin.close();

                        // 可以对父窗口进行刷新 
                        xadmin.father_reload();
                    });
		   		}else{
		   			alert(res.msg);
		   		}
           },
		   error:function (XMLHttpRequest, textStatus, errorThrown) {
        	   		alert(XMLHttpRequest);
        	   		alert(textStatus);
        	   		alert(errorThrown);
           }
           
       });
}

function getParam(variable){
    var query = window.location.search.substring(1);
    var vars = query.split("&");
    for (var i=0;i<vars.length;i++) {
            var pair = vars[i].split("=");
            if(pair[0] == variable){return pair[1];}
    }
    return(false);
}

function init(){
	var id=getParam("id");
	$.ajax({
     url:'/mgr/goods/'+id, /*接口域名地址*/
     type:'get',
     data: {},
     success:function(res){
         //console.log(res.data);
         if(res.code==0){
         	$("#id").val(res.data.id);
         	$("#price").val(res.data.purchase_price);
         }else{
             layer.alert(res.msg, {icon: 0,title: '提示'})
         }
     },
     error:function (XMLHttpRequest, textStatus, errorThrown) {
	   		alert(XMLHttpRequest);
	   		alert(textStatus);
	   		alert(errorThrown);
		}
 })
}
init()
</script>
    </body>

</html>